package com.canbot.u05.adapter.training;

import android.app.ActionBar;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.canbot.u05.R;

import java.util.List;

/**
 *
 */

public class TrainingListViewAdapter extends BaseAdapter implements View.OnClickListener {

        private List<String> dList;

        private Context context;

        private LayoutInflater mInflater;

        private PopupWindow popupWindow;

        private int i;


        public TrainingListViewAdapter(Context context, List<String> dList) {
                super();
                this.context = context;
                mInflater = LayoutInflater.from(context);
                this.dList = dList;
        }

        @Override
        public int getCount() {
                return dList.size();
        }

        @Override
        public String getItem(int position) {
                return dList.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        //然后重写getView
        @Override
        public View getView(final int position, View view, ViewGroup parent) {

                if (view == null) {
                        view = mInflater.inflate(R.layout.training_host_item, null);
                        Log.e("TAG", "" + view);
                }

                TextView info = (TextView) view.findViewById(R.id.host_name);
                info.setText(dList.get(position));

                view.findViewById(R.id.img1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                showPopupWindow(view, position,dList.get(position));
                        }
                });

                view.findViewById(R.id.rel_training).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                if (listner != null) {
                                        listner.doTask(position);
                                }
                        }
                });
                return view;
        }

        //弹框的显示
        public void showPopupWindow(View view, int position,String text) {
                i = position;
                // 一个自定义的布局，作为显示的内容
                View contentView = LayoutInflater.from(context).inflate(R.layout.training_host_item_menu, null);
                Log.e("contentView", contentView + ""+ text );

                // 设置按钮的点击事件
                TextView info = (TextView) contentView.findViewById(R.id.text_name);
                info.setText(text);

                Button buttonname = (Button) contentView.findViewById(R.id.btn_name);
                Button buttondelete = (Button) contentView.findViewById(R.id.btn_delete);

                info.setOnClickListener(this);
                buttonname.setOnClickListener(this);
                buttondelete.setOnClickListener(this);

                popupWindow = new PopupWindow(contentView,
                        ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);


                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                        R.drawable.training_menu_bg));

                // 设置好参数之后再show
                //popupWindow.showAsDropDown(view);

                int[] location = new int[2];
                view.getLocationOnScreen(location);
                popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x182),-(int)context.getResources().getDimension(R.dimen.x30));
                if (listner != null) {
                        listner.showPopuwindow(position);
                }

        }

        public interface AdapterOnclickListner {

                void onclick(int id);

                void showPopuwindow(int position);

                void doTask(int id);
        }

        AdapterOnclickListner listner;

        public void setListner(AdapterOnclickListner listner) {
                this.listner = listner;
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_name:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_name);
                                        }
                                }
                                break;
                        case R.id.btn_delete:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_delete);
                                        }
                                }

                                break;
                        case R.id.text_name:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.doTask(i);
                                        }
                                }

                                break;
                        default:
                                break;

                }
        }
}
